 /**
 * Description: 系统附件业务实现类
 * Copyright:Copyright 2018 nfky.com. All rights reserved
 * @author:xk
 * @since:0.0.1
 * Create at:2018-01-10 下午 19:40:18
 *  
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2018-01-10   xk       0.0.1     Initial
 */
package com.metro.activation.sys.service.impl;

import com.metro.core.dao.IMapper;
import com.metro.core.service.BaseServiceSupport;
import com.metro.core.service.ServiceException;
import com.metro.core.vo.ResultCode;
import com.metro.core.vo.inf.IResult;
import com.metro.activation.common.beans.constant.PropertiesEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.metro.activation.sys.mapper.ISysAttachmentMapper;
import com.metro.activation.sys.service.inf.ISysAttachmentService;
import com.metro.activation.sys.beans.vo.SysAttachment;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

 /**
 * 系统附件业务实现类<br>
 *
 * @author xk
 * @since [0.0.1]
 * @version [0.0.1, 2018-01-10]
 */
@Service("sysAttachmentService")
public class SysAttachmentService extends BaseServiceSupport<SysAttachment> implements ISysAttachmentService{
    //系统附件Mapper
    @Autowired
    private ISysAttachmentMapper sysAttachmentMapper;

    @Override
    public IMapper getMapper() {
        return sysAttachmentMapper;
    }

     @Override
     public void download(String filePath, HttpServletResponse response) {
         try {
             String rootDir = PropertiesEnum.getPropertyValue(PropertiesEnum.FILE_UPLOAD_ROOT);
             String realPath = rootDir + "/" + filePath;
             // path是指欲下载的文件的路径。
             File file = new File(realPath);
             // 取得文件名。
             String filename = file.getName();
             // 取得文件的后缀名。
             String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();

             // 以流的形式下载文件。
             InputStream fis = new BufferedInputStream(new FileInputStream(file));
             byte[] buffer = new byte[fis.available()];
             fis.read(buffer);
             fis.close();
             // 清空response
             response.reset();
             // 设置response的Header
             response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
             response.addHeader("Content-Length", "" + file.length());
             OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
             response.setContentType("application/octet-stream");
             toClient.write(buffer);
             toClient.flush();
             toClient.close();
         } catch (Exception e) {
             log.error("下载文件失败", e);
             throw new ServiceException(ResultCode.ERROR);
         }
     }

     @Override
     public void download(Long fileId, HttpServletResponse response) {
         try {
             SysAttachment attachment = sysAttachmentMapper.getById(fileId);
             download(attachment.getPath(), response);
         } catch (Exception e) {
             log.error("下载文件失败", e);
             throw new ServiceException(ResultCode.ERROR);
         }
     }

     @Override
    public IResult save(SysAttachment entity) {
        try {
            sysAttachmentMapper.save(entity);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("新增系统附件失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult update(SysAttachment entity) {
        try {
            sysAttachmentMapper.update(entity);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("修改系统附件失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult delete(Long id) {
        try {
            sysAttachmentMapper.delete(id);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("删除失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult deleteByIds(String ids) {
        try {
            sysAttachmentMapper.deleteByIds(ids);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("删除失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }
}